﻿/* Copyright (C) Siemens AG, 2012. All Rights Reserved.
 * Author: Roland Eckl, Corporate Technology
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.ServiceModel;


namespace RVM.Infrastructure.Communication.WCF
{
    /// <summary>
    /// This ServiceHost allows to specify services with non-default constructors.
    /// The associated ExtendedInstanceProvider is added as Behavior and will create
    /// the service with additional arguments through reflection on each request.
    /// </summary>
    public class ExtendedServiceHost : ServiceHost
    {
        /// <summary>
        /// Default constructor of the service host.
        /// Allows to specify service type and base addresses, in addition arbitrary arguments.
        /// Note: baseAddresses have to be specified as array and not in params-style.
        /// </summary>
        /// <param name="serviceType">Type of service to create (create per request, not a singleton service)</param>
        /// <param name="baseAddresses">Array or base addresses</param>
        /// <param name="arguments">Arbitrary arguments. Note: Invalid type/number of arguments can lead to an exception when an instance shall be created on request!</param>
        public ExtendedServiceHost(Type serviceType, Uri[] baseAddresses, params object[] arguments) : base(serviceType, baseAddresses)
        {
            // Simply add behavior with the ExtendedInstanceProvider.
            foreach (var cd in ImplementedContracts.Values)
            {
                cd.Behaviors.Add(new ExtendedInstanceProvider(serviceType, arguments));
            }
        }
    }
}
